package com.oot2d.ui;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class Items {
    //Array of all current inventory items
    private static byte[] items;

    //Array of items mapped to C buttons
    private static int[] citems;

    //Spritesheet for item icons
    private static SpriteSheet icons;

    //These ints indicate the item's position in the menu
    //as well as in the items array
    public static final int NOTHING = -1;
    public static final int DEKU_STICK = 0;
    public static final int DEKU_NUT = 1;
    public static final int BOMBS = 2;
    public static final int BOW = 3;
    public static final int FIRE_ARROW = 4;
    public static final int DINS_FIRE = 5;
    public static final int SLINGSHOT = 6;
    public static final int OCARINA = 7;
    public static final int BOMBCHU = 8;
    public static final int SHOT = 9;
    public static final int ICE_ARROW = 10;
    public static final int FARORES_WIND = 11;
    public static final int BOOMERANG = 12;
    public static final int LENS_OF_TRUTH = 13;
    public static final int MAGIC_BEANS = 14;
    public static final int MEGATON_HAMMER = 15;
    public static final int LIGHT_ARROW = 16;
    public static final int NAYRUS_LOVE = 17;
    public static final int BOTTLE_ONE = 18;
    public static final int BOTTLE_TWO = 19;
    public static final int BOTTLE_THREE = 20;
    public static final int BOTTLE_FOUR = 21;
    public static final int QUEST_ONE = 22;
    public static final int QUEST_TWO = 23;

    //These are sub-items; different versions of items
    //that go in the same slot as another
    public static final byte FAIRY_BOW = 0;
    public static final byte FIRE_BOW = 1;
    public static final byte ICE_BOW = 2;
    public static final byte LIGHT_BOW = 3;

    public static final byte FAIRY_OCARINA = 0;
    public static final byte OCARINA_OF_TIME = 1;

    public static final byte HOOK_SHOT = 0;
    public static final byte LONG_SHOT = 1;

    public static final byte EMPTY = 0;
    public static final byte RED_POTION = 1;
    public static final byte GREEN_POTION = 2;
    public static final byte BLUE_POTION = 3;
    public static final byte FAIRY = 4;
    public static final byte MILK_WHOLE = 5;
    public static final byte MILK_HALF = 6;
    public static final byte FISH = 7;
    public static final byte RUTO_LETTER = 8;
    public static final byte BLUE_FIRE = 9;
    public static final byte BUGS = 10;
    public static final byte POE_SOUL = 11;
    public static final byte BIG_POE_SOUL = 12;

    //Quest items (slot 1)
    public static final byte POCKET_EGG = 0;
    public static final byte POCKET_CUCCO = 1;
    public static final byte COJIRO = 2;
    public static final byte MUSHROOM = 3;
    public static final byte MUSHROOM_POTION = 4;
    public static final byte POACHERS_SAW = 5;
    public static final byte BROKEN_GORON_SWORD = 6;
    public static final byte PRESCRIPTION = 7;
    public static final byte FROG = 8;
    public static final byte EYEDROPS = 9;
    public static final byte SWORD_CERTIFICATE = 10;

    //Quest items (slot 2)
    public static final byte WEIRD_EGG = 0;
    public static final byte CHICKEN = 1;
    public static final byte ZELDA_LETTER = 2;
    public static final byte KEATON_MASK = 3;
    public static final byte SKULL_MASK = 4;
    public static final byte SPOOKY_MASK = 5;
    public static final byte BUNNY_HOOD = 6;
    public static final byte GORON_MASK = 7;
    public static final byte ZORA_MASK = 8;
    public static final byte GERUDO_MASK = 9;
    public static final byte MASK_OF_TRUTH = 10;

    public static void init()
    {
        try {
            icons = new SpriteSheet("res/ui/icons.png", 26, 26);
        } catch (SlickException e) {
            System.err.println(e.getMessage());
        }

        items = new byte[24];
        citems = new int[3];

        for (int i = 0; i < 24; i++)
            items[i] = NOTHING;

        for (int i = 0; i < 3; i++)
            citems[i] = NOTHING;

        //Load inventory from File class here
    }

    //Check if we have an item in this item slot
    public static boolean hasItem(int itemSlot)
    {
        return (items[itemSlot] > NOTHING);
    }

    //Get the value of an item in a specified slot
    //(useful for getting sub-items: which ocarina do we have? etc)
    public static byte getItem(int itemSlot)
    {
        return items[itemSlot];
    }

    //Return the item slot equipped to a C button
    public static int getCItemSlot(int slot)
    {
        return citems[slot];
    }

    public static int getCItem(int slot)
    {
        return items[citems[slot]];
    }

    //Set the value of an item in a specified slot
    public static void setItem(int itemSlot, byte value)
    {
        items[itemSlot] = value;
    }

    //Equip an item to a C button
    public static void setCItem(int itemSlot, int index)
    {
        if (items[itemSlot] == NOTHING)
            return;

        int n = -1;
        for (int i = 0; i < 3; i++) {
            if (citems[i] == itemSlot) {
                n = i;
                break;
            }
        }

        //Item already equipped to requested slot; do nothing
        if (n == index)
            return;

        //If the item is already equipped to another slot, swap
        if (n != -1) {
            if (citems[index] != NOTHING)
                citems[n] = citems[index];
            else
                citems[n] = NOTHING;
        }

        //Equip the item to the requested slot
        citems[index] = itemSlot;
    }

    public static void drawItem(int itemSlot, float x, float y)
    {
        if (items[itemSlot] == -1)
            return;

        int icon = itemSlot;

        if (itemSlot > 18 && itemSlot < 22)
            icon = 18;

        if (itemSlot > 21)
            icon -= 3;

        icons.startUse();
        icons.getSubImage(icon, items[itemSlot]).drawEmbedded(x, y);
        icons.endUse();
    }

    public static void test()
    {
        items[DEKU_STICK] = 0;
        items[DEKU_NUT] = 0;
        items[BOMBS] = 0;
        items[SLINGSHOT] = 0;
        items[OCARINA] = OCARINA_OF_TIME;
        items[BOW] = FAIRY_BOW;
        items[FIRE_ARROW] = 0;
        items[DINS_FIRE] = 0;
        items[NAYRUS_LOVE] = 0;
        items[BOMBCHU] = 0;
        items[SHOT] = HOOK_SHOT;
        items[ICE_ARROW] = 0;
        items[FARORES_WIND] = 0;
        items[BOOMERANG] = 0;
        items[LENS_OF_TRUTH] = 0;
        items[MAGIC_BEANS] = 0;
        items[MEGATON_HAMMER] = 0;
        items[LIGHT_ARROW] = 0;
        items[BOTTLE_ONE] = EMPTY;
        items[BOTTLE_TWO] = EMPTY;
        items[BOTTLE_THREE] = EMPTY;
        items[BOTTLE_FOUR] = FAIRY;
        items[QUEST_ONE] = WEIRD_EGG;
        items[QUEST_TWO] = BUNNY_HOOD;
    }
}
